<template>
  <div class="box">
    <div :id="id" :style="{height:height,width:width} " />
  </div>
</template>

<script>
import * as echarts from 'echarts'
import {getUserNumberList} from "../../api/user";
export default {
  name:'UserPieChart',
  props: {
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '600px'
    },
    height: {
      type: String,
      default: '400px'
    }
  },
  data() {
    return {
      chart: null,
      echartsData:[
    ],
    }
  },
  created() {
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
      this.getEchartsData()
    })
  },
  beforeUnmount() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    //得到图表数据
    getEchartsData(){
      getUserNumberList().then(resopnse => {
        this.echartsData = resopnse.data
        this.chart.setOption({
          series: [
            {
              data: this.echartsData
            }
          ]
        })
      })

    },
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      this.chart.setOption({
        title: {
          text: '平台使用用户数据',
          subtext: '人数',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '人数',
            type: 'pie',
            radius: '50%',
            data: this.echartsData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      })
    }
  }
}
</script>

<style scoped>
.box {
  padding-top: 40px;
  box-shadow: 0 8px 16px 0 rgb(144, 193, 238);
  /*background-color: #162d7d;*/
  /*background-color: #2c3e50;*/
}
.box:hover {
  /*box-shadow: 0 8px 16px 0 rgb(21, 20, 20);*/
}
</style>
